<?php
namespace app\common\curl;
header("Content-type: text/html; charset=utf-8");

/**
* 模拟请求类
*/
class Curl {
	
	public $ip;
	public $header_ip;
	public $header_host;
	public $agent;

	public function __construct() {
		$ip = '211'.'.'.mt_rand(64,71).'.'.mt_rand(0,255).'.'.mt_rand(0,255);
		$this->ip = $ip;
		$this->header_ip = array(
		'CLIENT-IP:'.$ip,
		'X-FORWARDED-FOR:'.$ip
		);
		$agents = array(
		'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',
		'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
		'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36',
		'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Mobile Safari/537.36',
		'Mozilla/5.0 (Windows NT 5.1; rv:22.0) Gecko/20100101 Firefox/22.0',
		'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5',
		'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; InfoPath.2; AskTbPTV/5.17.0.25589; Alexa Toolbar)',
		'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; Alexa Toolbar)',
		'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
		);
		$this->agents = $agents;
		$this->agent = $agents[mt_rand(0,8)];
	}

	/* 基本演示 */
	public function example() {
		$post_data = array(
		'user' => 'wang',
		'pwd' => '123456'
		);
		$referer = 'http://www.88.com';
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://localhost/Server.php');
		//加上这个表示执行curl_exec是把输出做为返回值,不会输出到浏览器
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		//如果页面遇到重定向则继续跳转
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		//伪造来源referer
		curl_setopt($ch, CURLOPT_REFERER, $referer);
		//伪造来源ip
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header_ip);
		//伪造agent
		curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
		//伪造cookie
		curl_setopt($ch, CURLOPT_COOKIE, 'user=admin');
		//提交post传参
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$out_put = curl_exec($ch);
		curl_close($ch);
		//输入结果
		return $out_put;
	}

	/* 模拟GET请求 */
	public function get($data=['url'=>'', 'referer'=>'', 'header'=>'' , 'cookie'=>'']) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $data['url']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		//如果页面遇到重定向则继续跳转
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		//伪造header
		if (!empty($data['header'])) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
		}else {
			//伪造来源ip
			curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header_ip);
		}
		//伪造来源referer
		if (!empty($data['referer'])) {
			curl_setopt ($ch, CURLOPT_REFERER, $data['referer']);
		}
		//伪造cookie
		if (!empty($data['cookie'])) {
			curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
		}
		$out_put = curl_exec($ch);
		curl_close($ch);
		return $out_put;
	}

	/* 模拟POST请求 */
	public function post($data=['url'=>'', 'referer'=>'', 'header'=>'', 'cookie'=>'', 'post_data'=>'']) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $data['url']);
		//把输出做为返回值,不会输出到浏览器
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		//如果页面遇到重定向则继续跳转
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		//伪造header
		if (!empty($data['header'])) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
		}else {
			//伪造来源ip
			curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header_ip);
		}
		//伪造来源referer
		if (!empty($data['referer'])) {
			curl_setopt($ch, CURLOPT_REFERER, $data['referer']);
		}
		//伪造cookie
		if (!empty($data['cookie'])) {
			curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
		}
		//提交post传参
		if (!empty($data['post_data'])) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data['post_data']);
		}
		$out_put = curl_exec($ch);
		curl_close ($ch);
		return $out_put;
	}

	/**
	 * 获取COOKIE
	 * @param return_type  返回类型：'array'或空则返回数组，'string'返回字符串
	 */
	public function getCookie($data=['url'=>'', 'referer'=>'', 'header'=>'', 'cookie'=>'', 'post_data'=>'', 'return_type'=>'']) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $data['url']);
		curl_setopt($ch, CURLOPT_HEADER,1); //将头文件的信息作为数据流输出
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流
		//伪造header
		if (!empty($data['header'])) {
			curl_setopt($ch, CURLOPT_HTTPHEADER, $data['header']);
		}else {
			//伪造来源ip
			curl_setopt($ch, CURLOPT_HTTPHEADER, $this->header_ip);
		}
		//伪造来源referer
		if (!empty($data['referer'])) {
			curl_setopt($ch, CURLOPT_REFERER, $data['referer']);
		}
		//伪造cookie
		if (!empty($data['cookie'])) {
			curl_setopt($ch, CURLOPT_COOKIE, $data['cookie']);
		}
		//提交post传参
		if (!empty($data['post_data'])) {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data['post_data']);
		}
		$content = curl_exec($ch); //执行curl并赋值给$content
		preg_match_all('/Set-Cookie:(.*);/iU',$content,$inarr); //正则匹配获取所有cookie
		$cookie = null;
		//选择返回类型
		if ($data['return_type']=='' || $data['return_type']=='array') {
			$arr = array();
			foreach ($inarr[1] as $val) {
				$cookie[trim(strstr($val, '=', true))] = substr(strrchr($val, '='), 1);
			}
		}else {
			$arr = array();
			foreach ($inarr[1] as $val) {
				$arr[] = $val;
			}
			if (count($arr) > 1) {
				$cookie = implode(';', $arr);
			}elseif (count($arr) == 1) {
				$cookie = $arr[0];
			}
		}
		
		curl_close($ch);
		return $cookie;
	}

	/**
	 * 保存远程图片到本地
	 * @param filename 图片保存在本地的路径+图片名称【不要加后缀!】，会自动加后缀
	 */
	public function saveImg($data=['url'=>'', 'filename'=>'']) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $data['url']);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        ob_start();
        curl_exec($ch);
        $content = ob_get_contents();
        $type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
        $suffix = '.jpg'; //默认保存后缀
        if ($type == 'image/png') {
        	$suffix = '.png';
        }elseif ($type == 'image/gif') {
        	$suffix = '.gif';
        }
        ob_end_clean();
        $filename = $data['filename'].$suffix;  
	    $fp = @fopen($filename,"a"); //将文件绑定到流
	    fwrite($fp,$content);
        return is_file($filename);
	}


}
